﻿#include <QFile>
#include "CMapWidget.h"
#include "CGlobal.h"

CMapWidget::CMapWidget(QString sSceneGuid, IFDev* pDev, CRBasicDataBase* pBasicData, QWidget *parent)
	: m_sSceneGuid(sSceneGuid),m_pDev(pDev),m_pBasicDataBase(pBasicData), CRCardBase(parent)
{
	ui.setupUi(this);
	m_pDevData = (CDevDataRob *)pDev->m_pDevData;
	m_bIsAutoMapCenter = false;
	setStyleSheetFile();
	initUi();
	m_pTimer = new QTimer(this);
	m_pTimer->setTimerType(Qt::TimerType::PreciseTimer);
	connect(m_pTimer, &QTimer::timeout, this, &CMapWidget::slotRefushData);
	m_pTimer->start(1000);
}

CMapWidget::~CMapWidget()
{
	m_pTimer->stop();
	if (m_pTimer != nullptr)
	{
		delete m_pTimer;
		m_pTimer = nullptr;
	}
}

void CMapWidget::setStyleSheetFile()
{
	QString sQssPath = ":/qss/styleSheet.css";
	QFile qss(sQssPath);
	qss.open(QFile::ReadOnly);
	this->setStyleSheet(qss.readAll());
}

void CMapWidget::initUi()
{
	QVector<QPointF>vecPoint;
	QVector<int>vecVal;
	//拿到巡检点数组
	QMap<QString, CPolPoint*> mapPolPoint = m_pBasicDataBase->m_pPolArea->m_mapPolPoint;
	QVector<CPolPoint*> vecPolPoint = mapPolPoint.values().toVector();
	qSort(vecPolPoint.begin(), vecPolPoint.end(), sortPoint);
	for (int i = 0; i < vecPolPoint.size(); i++)
	{
		CPolPoint* pPoint = vecPolPoint[i];
		double dPointLocation = pPoint->m_dPointLocation;//巡检点位置,从原点算，路径的总长度
		double dPointX = pPoint->m_dPointX;//巡检点X坐标（在地图中）
		double dPointY = pPoint->m_dPointY;//巡检点Y坐标（在地图中）
		double dPointZ = pPoint->m_dPointZ;//巡检点Z坐标（在地图中）
		vecPoint.push_back(QPointF(dPointX, dPointY));
		vecVal.push_back(dPointLocation);
	}
	ui.widget->setPoints(vecPoint, vecVal);
}

void CMapWidget::slotRefushData()
{
	if (isVisible())
	{
		QVector<QPointF>vecPoint;
		QVector<int>vecVal;
		//拿到巡检点数组
		m_pBasicDataBase->m_pMutex->lockAll();
		QMap<QString, CPolPoint*> mapPolPoint = m_pBasicDataBase->m_pPolArea->m_mapPolPoint;
		QVector<CPolPoint*> vecPolPoint = mapPolPoint.values().toVector();
		qSort(vecPolPoint.begin(), vecPolPoint.end(), sortPoint);
		for (int i = 0; i < vecPolPoint.size(); i++)
		{
			CPolPoint* pPoint = vecPolPoint[i];
			double dPointLocation = pPoint->m_dPointLocation;//巡检点位置,从原点算，路径的总长度
			double dPointX = pPoint->m_dPointX;//巡检点X坐标（在地图中）
			double dPointY = pPoint->m_dPointY;//巡检点Y坐标（在地图中）
			double dPointZ = pPoint->m_dPointZ;//巡检点Z坐标（在地图中）
			vecPoint.push_back(QPointF(dPointX, dPointY));
			vecVal.push_back(dPointLocation);
		}
		bool bCharge = false;
		if (m_pDevData->m_sDevWorkStatus.get().toInt() == 3)
		{
			bCharge = true;
		}
		else
		{
			bCharge = false;
		}

		ui.widget->setPoints(vecPoint, vecVal);
		ui.widget->setActPoints(m_pDevData->m_sRobCurrLoca.get().toFloat(), bCharge);
		if (m_bIsAutoMapCenter == false)
		{
			ui.widget->autoAdjustMapCenter();
			m_bIsAutoMapCenter = true;
		}
		m_pBasicDataBase->m_pMutex->unLockAll();
	}
}
